using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Microsoft.SharePoint;

namespace MessageBoardControl
{
	/// <summary>
	/// Summary description for GroupList.
	/// </summary>
	[DefaultProperty("Text"), 
		ToolboxData("<{0}:PostList runat=server></{0}:PostList>")]
	public class PostList : System.Web.UI.WebControls.WebControl
	{
		#region Properties
        public SPWeb web;

		private string text;
	
		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("")] 
		public string Text 
		{
			get
			{
				return text;
			}

			set
			{
				text = value;
			}
		}

		private string url;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URL 
		{
			get
			{
				return url;
			}

			set
			{
				url = value;
			}
		}

		private string postSecurityUrl;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string PostSecurityUrl 
		{
			get
			{
				return postSecurityUrl;
			}

			set
			{
				postSecurityUrl = value;
			}
		}

		private string editThreadURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string EditThreadURL 
		{
			get
			{
				return editThreadURL;
			}

			set
			{
				editThreadURL = value;
			}
		}

		private string deleteThreadURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string DeleteThreadURL 
		{
			get
			{
				return deleteThreadURL;
			}

			set
			{
				deleteThreadURL = value;
			}
		}

		private string lockThreadURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string LockThreadURL 
		{
			get
			{
				return lockThreadURL;
			}

			set
			{
				lockThreadURL = value;
			}
		}

		private string editPostURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string EditPostURL 
		{
			get
			{
				return editPostURL;
			}

			set
			{
				editPostURL = value;
			}
		}

		private string deletePostURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string DeletePostURL 
		{
			get
			{
				return deletePostURL;
			}

			set
			{
				deletePostURL = value;
			}
		}


		private MessageBoard.Thread thread;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Thread Thread
		{
			get
			{
				return thread;
			}

			set
			{
				thread = value;
			}
		}

		private MessageBoard.User currentUser;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.User CurrentUser 
		{
			get
			{
				return currentUser;
			}

			set
			{
				currentUser = value;
			}
		}

		private string emotionsFolder;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string EmotionsFolder 
		{
			get
			{
				return emotionsFolder;
			}

			set
			{
				emotionsFolder = value;
			}
		}

		private string imagesFolder;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string ImagesFolder 
		{
			get
			{
				return imagesFolder;
			}

			set
			{
				imagesFolder = value;
			}
		}

		private string displayImagesFolder;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string DisplayImagesFolder 
		{
			get
			{
				return displayImagesFolder;
			}

			set
			{
				displayImagesFolder = value;
			}
		}

		private Boolean viewDisplayImages;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public Boolean ViewDisplayImages 
		{
			get
			{
				return viewDisplayImages;
			}

			set
			{
				viewDisplayImages = value;
			}
		}

		private MessageBoard.Language language;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Language Language 
		{
			get
			{
				return language;
			}

			set
			{
				language = value;
			}
		}
		#endregion

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
			thread.TotalViews++;
			thread.Update();

			CurrentUser.MakeRequest();

			output.WriteBeginTag("table");
			output.WriteAttribute("width", "100%");
			output.WriteAttribute("cellpadding", "0");
			output.WriteAttribute("cellspacing", "0");
			output.RenderBeginTag(HtmlTextWriterTag.Tr);
			output.WriteBeginTag("td");
			output.WriteAttribute("class", "Forum_TableBorder");
			output.Write(HtmlTextWriter.TagRightChar);

			output.WriteBeginTag("table");
			output.WriteAttribute("width", "100%");
			output.WriteAttribute("cellspacing", "1");
			output.WriteAttribute("cellpadding", "2");
				output.RenderBeginTag(HtmlTextWriterTag.Tr);

					output.WriteBeginTag("td");
					output.WriteAttribute("colspan", "2");
					output.WriteAttribute("class", "Forum_Title1");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write("<img src="+imagesFolder+"/icon"+thread.Icon+".gif border=0> ");
						output.Write(thread.Subject);
					output.WriteEndTag("td");

				output.RenderEndTag();

				output.RenderBeginTag(HtmlTextWriterTag.Tr);

					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title2");
					output.WriteAttribute("colspan", "2");
						output.Write(HtmlTextWriter.TagRightChar);
						output.WriteBeginTag("div");
						output.WriteAttribute("class", "Forum_BoldText");
						output.Write(HtmlTextWriter.TagRightChar);
							output.Write(Translate("Author"));
						output.WriteEndTag("div");
					output.WriteEndTag("td");


				output.RenderEndTag();

				foreach(MessageBoard.Post post in MessageBoard.Post.GetAll(thread))
				{
					if(MessageBoard.Role.Check(currentUser, post, MessageBoard.Role.Permission.CanViewPosts) == MessageBoard.Role.Allow.Yes)
					{
						output.RenderBeginTag(HtmlTextWriterTag.Tr);

							output.WriteBeginTag("td");
							output.WriteAttribute("colspan", "1");
							output.WriteAttribute("class", "Forum_Title3");
							output.WriteAttribute("width", "120");
							output.WriteAttribute("vAlign", "top");
							output.Write(HtmlTextWriter.TagRightChar);							
								output.Write("<img src="+imagesFolder+"/user_Is"+(post.User.Loged==true?"Online":"Offline")+".gif border=0 title='"+(post.User.Loged==true?Translate("Online"):Translate("Offline"))+"'> ");							
								output.WriteBeginTag("a");
								output.WriteAttribute("href", URL+"Forum=PostList&ThreadId="+thread.Id);
								output.Write(HtmlTextWriter.TagRightChar);
                                post.User.web = web;		
									output.Write(post.User.DisplayName);
									if(ViewDisplayImages==true)
									{
										output.Write("<br><br>");
										output.Write("<img src="+displayImagesFolder+"/"+post.User.DisplayImage+".gif border=0 width=50 height=50>");	
									}
								output.WriteEndTag("a");
								output.Write("<br><br>");
								output.Write("<span class='Forum_BoldText'>"+Translate("Total Posts")+":</span> "+post.User.TotalPosts);
								output.Write("<br><br>");
							output.WriteEndTag("td");


							output.WriteBeginTag("td");
							output.WriteAttribute("class", "Forum_Title4");
							output.WriteAttribute("valign", "top");
							output.Write(HtmlTextWriter.TagRightChar);
								output.WriteBeginTag("table");
								output.WriteAttribute("cellspacing", "0");
								output.WriteAttribute("cellpadding", "2");
								output.WriteAttribute("width", "100%");
									output.RenderBeginTag(HtmlTextWriterTag.Tr);

										output.WriteBeginTag("td");
										output.WriteAttribute("class", "Forum_Title3");
										output.WriteAttribute("width", "99%");
										output.Write(HtmlTextWriter.TagRightChar);
											output.Write("<span class='Forum_BoldText'>"+Translate("Posted")+":</span> "+ post.PostDate.ToLongDateString() + " " + post.PostDate.ToLongTimeString());
										output.WriteEndTag("td");

										output.WriteBeginTag("td");
										output.WriteAttribute("class", "Forum_Title3");
										output.WriteAttribute("width", "1%");
										output.Write(HtmlTextWriter.TagRightChar);
											output.Write("<nobr>");
											if(post.IsFirstPostInThread==true)
											{
												if(MessageBoard.Role.Check(currentUser, thread, MessageBoard.Role.Permission.CanEditThreads) == MessageBoard.Role.Allow.Yes || (MessageBoard.Role.Check(currentUser, post, MessageBoard.Role.Permission.CanEditHisThreads) == MessageBoard.Role.Allow.Yes && post.User.Id == currentUser.Id))
												{
													output.Write("<a href="+EditThreadURL+"Forum=EditThread&ThreadId="+post.Thread.Id+"><img src="+imagesFolder+"/icon_edit_topic.gif border=0 title='"+Translate("Edit the thread")+"'></a> ");
												}
												if((MessageBoard.Role.Check(currentUser, thread, MessageBoard.Role.Permission.CanDeleteThreads) == MessageBoard.Role.Allow.Yes) || (MessageBoard.Role.Check(currentUser, post, MessageBoard.Role.Permission.CanDeleteHisThreads) == MessageBoard.Role.Allow.Yes && post.User.Id == currentUser.Id))
												{													
													output.Write("<a href="+DeleteThreadURL+"Forum=DeleteThread&ThreadId="+post.Thread.Id+"><img src="+imagesFolder+"/icon_delete_reply.gif border=0 title='"+Translate("Delete the Thread")+"'></a> ");
												}
												if(MessageBoard.Role.Check(currentUser, thread, MessageBoard.Role.Permission.CanEditThreads) == MessageBoard.Role.Allow.Yes || (MessageBoard.Role.Check(currentUser, post, MessageBoard.Role.Permission.CanEditHisThreads) == MessageBoard.Role.Allow.Yes && post.User.Id == currentUser.Id))
												{
													if(thread.Locked)
													{
														output.Write("<a href="+LockThreadURL+"Forum=LockThread&ThreadId="+post.Thread.Id+"><img src="+imagesFolder+"/flock.gif border=0 title='"+Translate("UnLock the thread")+"'></a> ");
													}
													else
													{
														output.Write("<a href="+LockThreadURL+"Forum=LockThread&ThreadId="+post.Thread.Id+"><img src="+imagesFolder+"/flock.gif border=0 title='"+Translate("Lock the thread")+"'></a> ");
													}
												}
												if(MessageBoard.Role.Check(currentUser, thread, MessageBoard.Role.Permission.CanManageThreadsSecurity) == MessageBoard.Role.Allow.Yes)
												{
													output.Write("<a href='"+postSecurityUrl+"Forum=ManageSecurity&ThreadId=" + thread.Id + "'><img src="+imagesFolder+"/Permissions.gif border=0 title='"+Translate("Message Thread Security")+"'></a>");
												}
											}
											else
											{
												if(MessageBoard.Role.Check(currentUser, post, MessageBoard.Role.Permission.CanEditPosts) == MessageBoard.Role.Allow.Yes || (MessageBoard.Role.Check(currentUser, post, MessageBoard.Role.Permission.CanEditHisPosts) == MessageBoard.Role.Allow.Yes && post.User.Id == currentUser.Id))
												{
													output.Write("<a href="+EditPostURL+"Forum=EditPost&PostId="+post.Id+"><img src="+imagesFolder+"/icon_edit_topic.gif border=0></a> ");
												}
												if(MessageBoard.Role.Check(currentUser, post, MessageBoard.Role.Permission.CanDeletePosts) == MessageBoard.Role.Allow.Yes || (MessageBoard.Role.Check(currentUser, post, MessageBoard.Role.Permission.CanDeleteHisPosts) == MessageBoard.Role.Allow.Yes && post.User.Id == currentUser.Id))
												{
													output.Write("<a href="+DeletePostURL+"Forum=DeletePost&PostId="+post.Id+"><img src="+imagesFolder+"/icon_delete_reply.gif border=0></a> ");
												}
												if(MessageBoard.Role.Check(currentUser, post, MessageBoard.Role.Permission.CanManagePostsSecurity) == MessageBoard.Role.Allow.Yes)
												{
													output.Write("<a href='"+postSecurityUrl+"Forum=ManageSecurity&PostId=" + post.Id + "'><img src="+imagesFolder+"/Permissions.gif border=0 title='"+Translate("Message Post Security")+"'></a>");
												}
											}
											output.Write("</nobr>");
										output.WriteEndTag("td");

									output.RenderEndTag();

									output.RenderBeginTag(HtmlTextWriterTag.Tr);
										output.WriteBeginTag("td");
										output.WriteAttribute("class", "Forum_Title4");
										output.WriteAttribute("colspan", "2");
										output.WriteAttribute("valign", "top");
										output.Write(HtmlTextWriter.TagRightChar);
											output.Write(Emotions.PutEmotions(post.Text, emotionsFolder));
										output.WriteEndTag("td");
									output.RenderEndTag();
								output.WriteEndTag("table");
							output.WriteEndTag("td");

							


						output.RenderEndTag();
					}
				}
				output.RenderBeginTag(HtmlTextWriterTag.Tr);
					output.WriteBeginTag("td");
					output.WriteAttribute("colspan", "2");
					output.WriteAttribute("class", "Forum_Title1");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write("&nbsp;");
					output.WriteEndTag("td");
				output.RenderEndTag();
			output.WriteEndTag("table");

			output.WriteEndTag("td");
			output.RenderEndTag();
			output.WriteEndTag("table");
		}

		private MessageBoard.CustomControl customControl = MessageBoard.CustomControl.Get("PostList");

		private string Translate(string word)
		{
			return MessageBoard.Translation.Get(customControl, word, Language).Equal;
		}
	}
}
